package com.lyj.io.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyj.io.common.constant.Constant;
import com.lyj.io.common.utils.DateUtil;
import com.lyj.io.module.sys.dao.SysMenuDao;
import com.lyj.io.module.sys.entity.SysMenu;
import com.lyj.io.module.sys.entity.SysPermission;
import com.lyj.io.module.sys.service.SysMenuService;
import com.lyj.io.module.sys.service.SysPermissionService;
import com.lyj.io.module.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜单接口
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/9/13 15:27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysPermissionService sysPermissionService;

    @Override
    public List<SysMenu> getUserMenuList(String userId) {
        //系统管理员，拥有最高权限
        if (userId.equals(Constant.SUPER_ADMIN)) {
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<String> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    private List<SysMenu> getAllMenuList(List<String> menuIdList) {
        //查询根菜单列表
        List<SysMenu> menuList = queryListParentId("0", menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);
        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<String> menuIdList) {
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();

        for (SysMenu entity : menuList) {
            //目录
            if (entity.getMenuType() == Constant.MenuType.CATALOG.getValue()) {
                entity.setChildren(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    @Override
    public List<SysMenu> queryListParentId(String parentId, List<String> menuIdList) {
        List<SysMenu> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }
        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenu> queryListParentId(String parentId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        queryWrapper.eq("parent_menu_id", parentId);
        queryWrapper.eq("delete_flag", "0");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void grantMenuToRole(SysPermission sysPermission) {
        SysPermission existPermission = sysPermissionService.getPermissionsByRoleId(sysPermission.getRoleId());
        if(null != existPermission){
            UpdateWrapper<SysPermission> updateWrapper = new UpdateWrapper<SysPermission>();
            updateWrapper.eq("role_id", sysPermission.getRoleId());
            updateWrapper.set("permission", sysPermission.getPermission());
            sysPermissionService.update(updateWrapper);
        }else{
            sysPermissionService.save(sysPermission);
        }
    }

    @Override
    public void removeByIds(List<String> menuIds) {
        UpdateWrapper<SysMenu> updateWrapper = new UpdateWrapper<SysMenu>();
        updateWrapper.in("menu_id", menuIds);
        updateWrapper.set("delete_flag", DateUtil.format2String(DateUtil.YYYYMMDDHHMMSS, new Date()));
        this.baseMapper.update(null, updateWrapper);
    }
}
